import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:honeypotflutter/modules/preferences/api/preferencesApi.dart';
import 'package:honeypotflutter/modules/preferences/model/preferences_listModel.dart';
import 'package:honeypotflutter/modules/preferences/page/preferences_add/view.dart';
import 'package:honeypotflutter/modules/preferences/view/preferences_listItem.dart';
import 'package:honeypotflutter/public/public_get.dart';
import 'package:honeypotflutter/publicBussine/bussine_image.dart';
import 'package:honeypotflutter/publicBussine/view/bussine_button.dart';
import 'package:honeypotflutter/public/views/public_appBar.dart';
import 'package:honeypotflutter/public/views/public_refresh.dart';
import 'preferences_search.dart';

class PreferencesListPage extends StatefulWidget {
  const PreferencesListPage({Key? key}) : super(key: key);
  @override
  State<PreferencesListPage> createState() => _PreferencesListPageState();
}

class _PreferencesListPageState extends State<PreferencesListPage> {
  int _page = 1;

  final HLRefreshController _refreshController = HLRefreshController(initialRefresh: true);

  /*
  * 去搜索
  * */
  _pushToSearch() async {
    try {
      bool? isDelete = await Get.to<bool>(PreferencesSearchPage());
      if (isDelete != null && isDelete == true) {
        _refreshController.requestRefresh();
      }
    } catch (e) {}
  }

  /*
  * 去添加
  * */
  _pushToAdd() async {
    try {
      bool? isAdd = await HLGet.to(PreferencesAddPage());
      if (isAdd != null && isAdd == true) {
        _refreshController.requestRefresh();
      }
    } catch (e) {}
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: HLAppBar(
        navigationTitle: "收件人偏好设置",
        actions: [
          IconButton(
              onPressed: _pushToSearch,
              icon: Image.asset(
                MGImage.image("public_search", type: ImagePathType.public),
                width: 20,
              ))
        ],
      ),
      body: HLRefresh<PreferencesListModel>(
          refreshController: _refreshController,
          onRefresh: () async {
            _page = 1;
            return await PreferencesApi.requestList(page: _page);
          },
          onLoading: () async {
            _page += 1;
            return await PreferencesApi.requestList(page: _page);
          },
          snap: (PreferencesListModel model, int index) {
            return PreferencesListItem(
              itemModel: model,
              deleteAction: () {
                _refreshController.requestRefresh();
              },
            );
          }),
      bottomSheet: MGButton(
        title: "添加用户",
        padding: EdgeInsets.only(left: 30, right: 30, bottom: 30, top: 15),
        buttonAction: _pushToAdd,
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
}
